package com.gy.zpchat.ui.friend.search.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import im.zego.zim.ZIM;
import im.zego.zim.callback.ZIMFriendsSearchedCallback;
import im.zego.zim.entity.ZIMError;
import im.zego.zim.entity.ZIMFriendInfo;
import im.zego.zim.entity.ZIMFriendSearchConfig;

import com.gy.zpchat.R;
import com.gy.zpchat.ui.friend.search.ui.adapter.SearchFriendListAdapter;
import java.util.ArrayList;

public class ZIMFriendSearchResultsActivity extends Activity implements ZIMFriendsSearchedCallback {

    public static void actionStart(Activity activity, String searchKeyword) {
        Intent intent = new Intent(activity, ZIMFriendSearchResultsActivity.class);
        intent.putExtra("searchKeyword", searchKeyword);
        activity.startActivity(intent);
    }

    private static final String TAG = "ZIMGroupSearchActivity";
    private static final int SEARCH_COUNT = 100;
    private String searchKeyword;
    private int nextFlag = 0;

    private final SearchFriendListAdapter friendListAdapter = new SearchFriendListAdapter();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_search_results);
        searchKeyword = getIntent().getStringExtra("searchKeyword");

        initView();
        ZIMFriendSearchConfig config = new ZIMFriendSearchConfig();
        config.count = SEARCH_COUNT;
        config.nextFlag = nextFlag;
        config.isAlsoMatchFriendAlias = true;
        config.keywords.add(searchKeyword);
        ZIM.getInstance().searchLocalFriends(config, this);
    }

    private void initView() {
        RecyclerView recyclerView = findViewById(R.id.member_list);
        LinearLayoutManager linearLayoutManager =
            new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(friendListAdapter);
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int lastVisibleItemPosition =
                    linearLayoutManager.findLastCompletelyVisibleItemPosition();
                int itemCount = friendListAdapter.getItemCount();
                if (lastVisibleItemPosition == itemCount - 1) {
                    onLoadMsg();
                }
            }
        });
    }

    public void onLoadMsg() {
        if (nextFlag != 0) {
            ZIMFriendSearchConfig config = new ZIMFriendSearchConfig();
            config.count = SEARCH_COUNT;
            config.nextFlag = nextFlag;
            config.isAlsoMatchFriendAlias = true;
            config.keywords.add(searchKeyword);
            ZIM.getInstance().searchLocalFriends(config, this);
        }
    }

    @Override
    public void onFriendsSearched(ArrayList<ZIMFriendInfo> friendInfoList, int nextFlag,
                                  ZIMError errorInfo) {
        this.nextFlag = nextFlag;
        friendListAdapter.add(friendInfoList);
    }
}
